package taskTracker.ui.lite;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import taskTracker.Project;

public class ProjectForm extends Form {
	/**Command for accepting changes when creating an existing project*/
	public final Command cmdCreate = new Command("Ok", Command.OK, 1);
	
	/**Command for accepting changes when updating an existing project*/
	public final Command cmdUpdate = new Command("Ok", Command.OK, 1);
	
	/**Command to cancel changes*/
	public final Command cmdCancel = new Command("Cancel", Command.BACK, 2);
	
	/**Field to set Project name*/
	private final TextField nameField = new TextField("Name", "", 64, TextField.ANY);
	/**Field to set Project notes*/
	private final TextField noteField = new TextField("Notes", "", 64, TextField.ANY);
	/**Field to set Project tags*/
	private final TextField tagField = new TextField("Tags", "", 64, TextField.ANY);
	
	/**The Project that is currently being edited*/
	private Project project;

	/**
	 * Initializes the dialog from a task list.
	 * If the given parameter is null, list is initialized for a new Project
	 * @param project
	 */
	public ProjectForm(Project project) {
		super("Add project");
		// Append form items
		super.append(nameField);
		super.append(noteField);
		super.append(tagField);
		// Add commands
		super.addCommand(project == null ? cmdCreate : cmdUpdate);
		super.addCommand(cmdCancel);
		// Initialize with the given project
		this.setProject(project);
	}

	/**
	 * Retrieves data of task list stored in this dialog
	 * @param project
	 * @return
	 */
	public Project getProject() {
		if (project == null) {
			project = new Project(nameField.getString(), noteField.getString());
		} else {
			project.setName(nameField.getString());
			project.setNotes(noteField.getString());
		}
		String tagString = tagField.getString();
		project.setTags(tagString);
		return project;
	}
	
	/**
	 * Sets data from a task list.
	 * @param project
	 */
	private void setProject(Project project) {
		if (project == null) {
			super.setTitle("New Project");
			nameField.setString("Name");
			noteField.setString("");
			tagField.setString("");
		} else {
			super.setTitle(project.getName());
			nameField.setString(project.getName());
			noteField.setString(project.getNotes());
			tagField.setString(project.getTagsAsString());
		}
		this.project = project;
	}

}
